package view;

import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.chart.plot.*;

import vossenenkonijnen.Actor;
import controller.Counter;
import controller.Field;
import controller.FieldStats;

import java.awt.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.JPanel;

/**
 * A barchart view of the simulation
 * @author Projectgroep 5
 *
 */
public class BarChart extends JPanel implements SimulatorView {
	
	// A map for storing colors for participants in the simulation
    private Map<Class, Color> colors;
    // A statistics object computing and storing simulation information
    private FieldStats stats;
    
    private ChartPanel chartPanel;

    public BarChart() {
        
    	stats = new FieldStats();
    	colors = new LinkedHashMap<Class, Color>();
    	
    	// put the chart into a panel
        chartPanel = new ChartPanel(createBarChart(new DefaultCategoryDataset()));
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(400, 270));
    	
    	this.add(chartPanel);
    	
	}
    
    /**
     * Create the BarChart view
     * @param dataset the data
     * @return BarChart
     */
    public JFreeChart createBarChart(DefaultCategoryDataset dataset) {
    	    	
		JFreeChart chart = ChartFactory.createBarChart
		("Staafdiagram vossen en konijnen","Actor", "Aantal", dataset, 
		PlotOrientation.VERTICAL, false,true, false);
		chart.setBackgroundPaint(Color.white);
		chart.getTitle().setPaint(Color.black); 
		CategoryPlot p = chart.getCategoryPlot(); 
		p.setRangeGridlinePaint(Color.red); 
		return chart;
    }
    
	@Override
	 public void setColor(Class animalClass, Color color)
	    {
	        colors.put(animalClass, color);
	    }
	 
	 @Override
	 public boolean isViable(Field field)
	    {
	        return stats.isViable(field);
	    }
	 
	 @Override
	 public void showStatus(int step, Field field)
	    {
	        if(!isVisible()) {
	            setVisible(true);
	        }
	        stats.reset();
	        for(int row = 0; row < field.getDepth(); row++) {
	            for(int col = 0; col < field.getWidth(); col++) {
	                Actor animal = (Actor)field.getObjectAt(row, col);
	                if(animal != null) {
	                    stats.incrementCount(animal.getClass());
	                }
	            }
	        }
	        stats.countFinished();
	        
	        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
	        
	        HashMap<Class, Counter> counters = stats.getActorAndCount(field);
	        for(Class key : counters.keySet()) {
	            Counter info = counters.get(key);
	            dataSet.setValue(info.getCount(), "Aantal", key.getSimpleName());
	        }
	        
	        JFreeChart chart = this.createBarChart(dataSet);
	        
	        this.chartPanel.setChart(chart);
	    }
	}
